package com.xindacloud.sms.portal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.portal.dao.SmsProjectMapper;
import com.xindacloud.sms.portal.entity.SmsProject;
import com.xindacloud.sms.portal.entity.SmsProjectMember;
import com.xindacloud.sms.portal.service.ISmsProjectMemberService;
import com.xindacloud.sms.portal.service.ISmsProjectService;
import com.xindacloud.sms.portal.vo.ProjectsResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SmsProjectServiceImpl extends ServiceImpl<SmsProjectMapper, SmsProject> implements ISmsProjectService {

    @Autowired
    ISmsProjectMemberService smsProjectMemberService;

    @Override
    public List<SmsProject> queryProjectListFor(List<String> projectIds) {
        return lambdaQuery()
                .in(CollectionUtils.isNotEmpty(projectIds), SmsProject::getProjectId, projectIds)
                .list();
    }

    @Override
    public List<ProjectsResultVO> queryProjectListById(Integer userId) {
        final List<SmsProjectMember> smsProjectMembers = smsProjectMemberService.queryProjectMemberList(userId);
        if (CollectionUtils.isEmpty(smsProjectMembers)) {
            return Collections.EMPTY_LIST;
        }
        final List<String> ids = smsProjectMembers.stream()
                .map(SmsProjectMember::getProjectId)
                .collect(Collectors.toList());
        final List<SmsProject> smsProjects = queryProjectListFor(ids);
        if (CollectionUtils.isEmpty(smsProjects)) {
            return Collections.EMPTY_LIST;
        }
        return smsProjects.stream()
                .map(mp -> {

                    ProjectsResultVO projectsResultVO = new ProjectsResultVO();
                    BeanUtils.copyProperties(mp, projectsResultVO);
                    return projectsResultVO;
                })
                .collect(Collectors.toList());
    }
}
